<?php
namespace app\recharge\model;

use think\Model;

class Store extends Model
{


    /**
     * 列表(主键列表，条件数组，闭包查询)
     *
     * @param    array $param 查询条件
     * @return   void
     */
    public function doList($param = null)
    {

        try {

            //查询数据
            //
            $result = self::all($param);

            $rows = [];

            foreach ($result as $data) {

                $item = $data->getData();
                $item['diamond_quantity'] = $this->getDiamondById($item['id']);
                $item['amount'] = (3 == $item['currency_type']) ? $item['price'] : null;
                array_push($rows, $item);
            }

            //返回结果
            //
            $res = array();

            $total = count($rows);

            if ($total > 0) {
                $res['rows'] = $rows;
                $res['total'] = $total;
            } else {
                $res['total'] = 0;
                $res['rows'] = array();
            }

            //请求类型返回
            return $res;

        } catch (\Exception $e) {
            throw new \think\Exception('异常消息:' . $e->getMessage());
        }
    }

    /**
     * 通过ID查询对应的钻石数
     *
     * @param    [INT] $id [主键]
     * @return   NULL|INT
     */
    public function getDiamondByRecord($record)
    {
        //$record = Store::getById($id);

        $sell_resources = $record->sell_resources;
        $gift_resources = $record->gift_resources;

        $count = 0;

        if (!empty($sell_resources)) {
            $arr = explode("=", $sell_resources);
            if ('102' == $arr[0]) {
                $count += (int) $arr[1];
            }
        }

        if (!empty($gift_resources)) {
            $arr = explode("=", $gift_resources);
            if ('102' == $arr[0]) {
                $count += (int) $arr[1];
            }
        }
        return $count;
    }

    /**
     * 订单物品金额信息
     *
     * @param    [type] $value [description]
     * @param    [type] $data [description]
     */
    public function getInfomationById($id)
    {

        $record = self::getById($id);
        $info = [];

        if ($record) {
            if (3 == $record->currency_type) {
                $info['amount'] = $record->price;
            }

            $diamond = $this->getDiamondByRecord($record);

            if ($diamond > 0) {
                $info['diamond'] = $diamond;
            }
        }
        return ($info);
    }
}
